package net.seehope.foodie.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.seehope.foodie.mapper.UserAddressMapper;
import net.seehope.foodie.pojo.UserAddress;
import net.seehope.foodie.pojo.bo.AddAddressBo;
import net.seehope.foodie.pojo.bo.UpdateBo;
import net.seehope.foodie.service.AddressService;

@Service
public class AddressServiceImpl implements AddressService{

	@Autowired
	private UserAddressMapper userAddressMapper;
	
	
	
	@Override
	public List<UserAddress> showAddressList(String userId) {
		UserAddress userAddress = new UserAddress();
		userAddress.setUserId(userId);
		
		return userAddressMapper.select(userAddress);
	}

	@Override
	public void setDefaultAddress(String userId, String addressId) {
		UserAddress userAddress = new UserAddress();
		
		//根据userId找到之前的默认地址，更改
		userAddress.setUserId(userId);
		userAddress.setIsDefault(1);
		UserAddress one = userAddressMapper.selectOne(userAddress);
		if(one!=null) {
			one.setIsDefault(0);
			userAddressMapper.updateByPrimaryKey(one);
			//根据userId和订单id找到当前地址，改为默认地址
			UserAddress userAddress2 = new UserAddress();
			userAddress2.setUserId(userId);
			userAddress2.setId(addressId);
			UserAddress one2 = userAddressMapper.selectOne(userAddress2);
			one2.setIsDefault(1);
			userAddressMapper.updateByPrimaryKey(one2);
		} else {
			UserAddress userAddress2 = new UserAddress();
			userAddress2.setUserId(userId);
			userAddress2.setId(addressId);
			UserAddress one2 = userAddressMapper.selectOne(userAddress2);
			one2.setIsDefault(1);
			userAddressMapper.updateByPrimaryKey(one2);
		}
		
		
		
	
	}

	@Transactional
	@Override
	public void updateAddress(UpdateBo bo) {
		UserAddress userAddress = new UserAddress();
		
		userAddress.setId(bo.getAddressId());	
		UserAddress one = userAddressMapper.selectOne(userAddress);
		
		one.setCity(bo.getCity());
		one.setDetail(bo.getDetail());
		one.setDistrict(bo.getDetail());
		one.setMobile(bo.getMobile());
		one.setProvince(bo.getProvince());
		one.setReceiver(bo.getReceiver());
		
		userAddressMapper.updateByPrimaryKey(one);
		
	}

	
	@Transactional
	@Override
	public void deleteAddress(String userId, String addressId) {
		UserAddress userAddress = new UserAddress();
		userAddress.setId(addressId);
		userAddress.setUserId(userId);
		userAddressMapper.delete(userAddress);
		
	}

	@Transactional
	@Override
	public void addAddress(AddAddressBo bo) throws IllegalAccessException, InvocationTargetException {
		UserAddress userAddress = new UserAddress();
		userAddress.setId(Sid.next());
		BeanUtils.copyProperties(userAddress, bo);
		
		UserAddress defaultAddress = new UserAddress();
		defaultAddress.setUserId(bo.getUserId());
		defaultAddress.setIsDefault(1);
		defaultAddress = userAddressMapper.selectOne(defaultAddress);
		if(defaultAddress!=null) {
			userAddress.setIsDefault(0);
		}else {
			userAddress.setIsDefault(1);
		}
		
		userAddress.setCreatedTime(new Date());
		userAddress.setUpdatedTime(new Date());
		userAddressMapper.insert(userAddress);
		
	}

	
	@Transactional
	@Override
	public UserAddress queryUserAddressByAddressId(String address) {
		return userAddressMapper.selectByPrimaryKey(address);
	}
	
	
	
	

}
